<%-- 
    Document   : Edit
    Created on : Jun 11, 2013, 8:52:44 PM
    Author     : Huong
--%>

<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>
            Project Management
        </h1>
        <h2>
            Edit project
        </h2>

        <%
            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;


            String pname = request.getParameter("x");

            if (pname != null) {

                String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=Workshop3-4;user=sa;password=sa";
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    con = DriverManager.getConnection(connectionUrl);
                    stmt = con.createStatement();
                    String SQL = "SELECT * FROM Puch WHERE Name='" + pname + "'";
                    rs = stmt.executeQuery(SQL);
                    rs.next();
                } catch (ClassNotFoundException ex) {
                    out.println("<h1>" + ex.getMessage() + "</h1>");
                } catch (SQLException ex) {
                    out.println("<h1>" + ex.getMessage() + "</h1>");
                }
            }
        %>
        <form method="POST">

            <table>
                <tr>
                    <td>
                        Project code
                    </td>
                    <td>
                        <%
                            out.println("<input type=\"text\" name=\"P_Code\" value= \"" + rs.getString("Code") + "\" /> ");
                        %>
                    </td>
                </tr>
                <tr>
                    <td>
                        Project name
                    </td>
                    <td>
                        <%
                            out.println("<input type=\"text\" name=\"P_Name\" value=\"" + rs.getString("Name") + "\" />");
                        %>
                    </td>
                </tr>
                <tr>
                    <td>
                        Status
                    </td>
                    <td>
                        <%
                            if (rs.getString("Status").equalsIgnoreCase("Active")) {
                                out.println("<input type=\"radio\" name=\"Active\" value=\"Active\" checked=\"checked\" /> Active"
                                        + "<input type=\"radio\" name=\"Active\" value=\"Inactive\" /> Inactive");
                            } else {
                                out.println("<input type=\"radio\" name=\"Active\" value=\"Active\" /> Active"
                                        + "<input type=\"radio\" name=\"Active\" value=\"Inactive\" checked=\"checked\" /> Inactive");
                            }
                        %>
                    </td>
                </tr>
                <tr>
                    <td>
                        Start date
                    </td>
                    <td>
                        <%
                            out.println("<input type=\"text\" name=\"S_Date\" value=\"" + rs.getDate("Start date") + "\" />");
                        %>
                    </td>
                </tr>
                <tr>
                    <td>
                        End Date
                    </td>
                    <td>
                        <%
                            out.println("<input type=\"text\" name=\"E_Date\" value=\"" + rs.getDate("End date") + "\" />");
                        %>
                    </td>
                </tr>
                <tr>
                    <td>
                        Employee
                    </td>
                    <td>
                        <%
                            out.println("<input type=\"text\" name=\"Emp\" value=\"" + rs.getString("Employee") + "\" />");
                        %>
                    </td>
                </tr>
                <tr>
                    <td>
                    <td>
                        <input type="submit" value="Save" />
                    </td>
                </tr>
            </table>
        </form>

        <jsp:useBean id = "project" class="myBean.Project"/>
        <jsp:setProperty name="project" property="*"/>
        <%
            String pcode = request.getParameter("P_Code");
            String pname2 = request.getParameter("P_Name");
            String status = request.getParameter("Active");
            int emp = Integer.parseInt(request.getParameter("Emp"));
            String sDate = request.getParameter("S_Date");
            String eDate = request.getParameter("E_Date");
            if (project.edit(pcode, pname2, status, sDate, eDate, emp)) {
                RequestDispatcher dispatcher = request.getRequestDispatcher("/studentList.jsp");
                dispatcher.forward(request, response);
            } else {
                RequestDispatcher dispatcher = request.getRequestDispatcher("/editStudent.jsp");
                dispatcher.forward(request, response);
            }
        %>
    </body>
</html>
